package cn.itcast.bos.system.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.base.action.BaseAction;
import cn.itcast.bos.domain.security.Role;
import cn.itcast.bos.system.service.RoleService;


@ParentPackage("json-default")
@Controller
@Namespace("/")
@Scope("prototype")
public class RoleAction extends BaseAction<Role>{
	private Integer[] permissionIds;
	private String menuIds;
	private Logger logger=LoggerFactory.getLogger(getClass());
	public void setPermissionIds(Integer[] permissionIds) {
		this.permissionIds = permissionIds;
	}
	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}
	@Autowired
	private RoleService roleService;
	@Action(value="show_role",results={@Result(name="success",type="json")})
	public String show(){
		List<Role> roles=roleService.show();
		ServletActionContext.getContext().getValueStack().push(roles);
		return SUCCESS;
	}
	
	@Action(value="add_role")
	public String add(){
		if(menuIds!=null){
			String[] strings = menuIds.split(",");
			Integer [] menus=new Integer[strings.length];
			for (int i = 0; i < strings.length; i++) {
				menus[i]=Integer.parseInt(strings[i]);
			}
			roleService.add(t,menus,permissionIds);
		}
		return NONE;
	}
}
